package com.scuec.groupfive.mapper;

import com.scuec.groupfive.entity.QuestionInfo;
import com.scuec.groupfive.entity.Questions;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface QuestionMapper {
    @Update("UPDATE questions SET " +
            "content = #{content}, " +
            "options = #{options}, " +
            "answer = #{answer}, " +
            "analysis = #{analysis}, " +
            "difficulty = #{difficulty}, " +
            "updated_at = NOW() " +
            "WHERE question_id = #{questionId}")
    int updateQuestionInfo(QuestionInfo questionInfo);
    /**
     * 根据题目ID查询题型（question_type）
     * @param questionId 题目ID（对应questions表的question_id字段）
     * @return 题型（如single_choice/fill_in_the_blank/subjective）
     */
    @Select("SELECT question_type FROM questions WHERE question_id = #{questionId}")
    String getQuestionTypeById(Long questionId);

    /**
     * 根据题目ID查询科目ID（subject_id）
     * @param questionId 题目ID（对应questions表的question_id字段）
     * @return 科目ID（对应subjects表的subject_id字段）
     */
    @Select("SELECT subject_id FROM questions WHERE question_id = #{questionId}")
    Integer getSubjectIdById(Long questionId);

    @Select("SELECT q.question_id as questionId, " +
            "q.subject_id as subjectId, " +
            "q.question_type as questionType, " +
            "q.content, " +
            "q.keyword, " +
            "q.creator_id as creatorId, " +
            "q.status, " +
            "q.type_id as typeId, " +
            "q.chapter_id as chapterId, " +
            "q.created_at as createdAt, " +
            "q.updated_at as updatedAt, " +
            "cq.options, " +
            "cq.answer as choiceAnswer, " +
            "cq.analysis as choiceAnalysis, " +
            "fbq.blanks, " +
            "fbq.answer_template as answerTemplate, " +
            "fbq.analysis as fillblankAnalysis, " +
            "sq.reference_answer as referenceAnswer, " +
            "sq.answer_hints as answerHints, " +
            "sq.analysis as subjectiveAnalysis, " +
            "f.favorite_id as favoriteId " +
            "FROM questions q " +
            "LEFT JOIN choice_questions cq ON q.question_id = cq.question_id AND q.question_type = 'single_choice' " +
            "LEFT JOIN fill_blank_questions fbq ON q.question_id = fbq.question_id AND q.question_type = 'fill_in_the_blank' " +
            "LEFT JOIN subjective_questions sq ON q.question_id = sq.question_id AND q.question_type = 'subjective' " +
            "LEFT JOIN favorites f ON q.question_id = f.question_id AND f.status = 1 " +
            "WHERE q.question_id = #{id}")
    Questions selectQuestionById(@Param("id") Long id);

    @Select("SELECT * FROM questions")
    List<Questions> selectAllQuestions();
}